{extend name="adminblue/base" /} {block name="resources"/}
<script src="ADMIN_JS/art_dialog.source.js"></script>
<script src="ADMIN_JS/iframe_tools.source.js"></script>
<script src="ADMIN_JS/material_managedialog.js"></script>
<script src="__STATIC__/js/ajax_file_upload.js" type="text/javascript"></script>
<script src="__STATIC__/js/file_upload.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="ADMIN_CSS/defau.css">
<style type="text/css">
.error {padding-left: 5px !important;}
</style>
{/block} 
{block name="main"}
<div class="space-10"></div>
<div class="set-style">
	<input type="hidden" value="{$brand_info.brand_id}" id="brand_id" />
	<dl>
		<dt><span class="color-red">*</span>品牌名称：</dt>
		<dd>
			<input id="brand_name" type="text" value="{$brand_info.brand_name}" placeholder="请输入品牌名称" class="input-common"/>
			<span class="error">请输入品牌名称</span>
		</dd>
	</dl>
	<dl>
		<dt><span class="color-red">*</span>品牌首字母：</dt>
		<dd>
			<input id="brand_initial" type="text" value="{$brand_info.brand_initial}" placeholder="请输入品牌首字母" class="input-common harf"/>
			<span class="error">请输入品牌首字母</span>
		</dd>
	</dl>
	<dl>
		<dt>是否推荐：</dt>
		<dd>
			<input id="brand_recommend" type="checkbox" class="checkbox" {if condition="$brand_info.brand_recommend == 1"}checked="checked" {/if} />
		</dd>
	</dl>
	<dl style="display: none;">
		<dt><span class="color-red"></span>商品分类：</dt>
		<dd>
			<!-- <div class="col-sm-2 category_name">
				<span>{$brand_info['category_name']}</span>
				<a href="javascript:void(0);" onclick="show_select()" class="btn btn-default">编辑</a>
			</div> -->
			<select id="category_id_1" name="account" class="form-control m-b select-common" onchange="changeCategory(1)">
				<option value="0">请选择...</option>
				{foreach name="goods_category_list" item="v1"}
				{if condition="$brand_info['category_id_1'] == $v1['category_id']"}
				<option value="{$v1['category_id']}" selected="selected">{$v1['category_name']}</option>
				{else/}
				<option value="{$v1['category_id']}">{$v1['category_name']}</option>
				{/if}
				{/foreach}
			</select>
			<select id="category_id_2" name="account" class="form-control m-b select-common" onchange="changeCategory(2)" style="display: none;">
				<option value="0">请选择...</option>
			</select>
			<select id="category_id_3" name="account" class="form-control m-b select-common" style="display: none;">
				<option value="0">请选择...</option>
			</select>
			<span class="error">请选择商品分类</span>
		</dd>
	</dl>
	<dl>
		<dt>图片：</dt>
		<dd>
			<div class="upload-btn-common">
				<div>
					<input class="input-file" name="file_upload" id="uploadBrand_pic" type="file" onchange="imgUpload(this);" title="上传">
					<input type="hidden" id="brand_pic" value="{$brand_info['brand_pic']}" />
				</div>
				<input type="text" id="text_brand_pic" class="input-common" readonly="readonly" value="{if condition="$brand_info.brand_pic"}{$brand_info.brand_pic}{/if}" />
				<em>上传</em>
				
				<img id="preview_brand_pic" src="__STATIC__/blue/img/upload-common-select.png" data-html="true" data-container="body" data-placement="top" data-trigger="manual" {if condition="$brand_info.brand_pic"}data-src="{:__IMG($brand_info.brand_pic)}"{/if}/>

			</div>
			<p class="hint">
				<span>建议使用<i class="important-note">宽200</i>像素-<i class="important-note">高60</i>像素内的<i class="important-note">GIF</i>或<i class="important-note">PNG</i>透明图片；点击下方"保存"按钮后生效。</span>
			</p>
		</dd>
	</dl>
	<dl>
		<dt>品牌推荐广告图：</dt>
		<dd>
			<div class="upload-btn-common">
				<div>
					<input class="input-file" name="file_upload" id="uploadBrand_ads" type="file" onchange="imgUpload(this);" title="上传">
					<input type="hidden" id="brand_ads" value="{$brand_info.brand_ads}" />
				</div>
				<input type="text" id="text_brand_ads" class="input-common" readonly="readonly" value="{if condition="$brand_info.brand_ads"}{$brand_info.brand_ads}{/if}" />
				<em>上传</em>
				
				<img id="preview_brand_ads" src="__STATIC__/blue/img/upload-common-select.png" data-html="true" data-container="body" data-placement="top" data-trigger="manual" {if condition="$brand_info.brand_ads"}data-src="{:__IMG($brand_info.brand_ads)}"{/if}/>

			</div>
			<p class="hint">
				<span>建议使用<i class="important-note">宽200</i>像素-<i class="important-note">高60</i>像素内的<i class="important-note">GIF</i>或<i class="important-note">PNG</i>透明图片；点击下方"保存"按钮后生效。</span>
			</p>
		</dd>
	</dl>
	<dl>
		<dt></dt>
		<dd>
			<button class="btn-common btn-big" onclick="editGoodsBrand();">保存</button>
			<button class="btn-common-cancle btn-big" onclick="javascript:history.back(-1);">返回</button>
		</dd>
	</dl>
</div>
<script>
var category_id_1 = "{$brand_info['category_id_1']}";
var category_id_2 = "{$brand_info['category_id_2']}";
var category_id_3 = "{$brand_info['category_id_3']}";
$(function(){
	if(category_id_1 != "" && category_id_1 != 0){
		changeCategory(1,function(){
			if(category_id_2 != "" && category_id_2 != 0) changeCategory(2);
		});
	}
});
function validation() {
	var brand_name = $("#brand_name");
	var brand_initial = $("#brand_initial");
	var category_id_1 = $("#category_id_1");
	var brand_pic = $("#brand_pic");
	var brand_ads = $("#brand_ads");
	if (brand_name.val() == "") {
		brand_name.focus().next("span").show();
		return false;
	} else {
		brand_name.next("span").hide();
	}

	if (brand_initial.val() == "") {
		brand_initial.focus().next("span").text("请输入品牌首字母").show();
		return false;
	} else if (brand_initial.val().length != 1) {
		brand_initial.focus().next("span").text("品牌首字母必须是单个字母").show();
		return false;
	} else {
		brand_initial.next("span").hide();
	}

	return true;
}

var flag = false;//防止重复提交
function editGoodsBrand() {
	if (!validation()) {
		return;
	}
	var brand_id = $("#brand_id").val();
	var brand_name = $("#brand_name").val();
	var brand_initial = $("#brand_initial").val();
	if ($("#brand_recommend").prop("checked")) {
		var brand_recommend = 1;
	} else {
		var brand_recommend = 0;
	}
	var category_id_1 = $("#category_id_1").val();
	var category_id_2 = $("#category_id_2").val();
	var category_id_3 = $("#category_id_3").val();
	var category_name = '';
	if (category_id_3 > 0) {
		category_name = $("#category_id_3 option:selected").text();
	} else if (category_id_2 > 0 && category_id_3 <= 0) {
		category_name = $("#category_id_2 option:selected").text();
	} else if (category_id_1 > 0) {
		category_name = $("#category_id_1 option:selected").text();
	} else {
		category_name = '';
	}
	var brand_pic = $("#brand_pic").val();
	var brand_ads = $("#brand_ads").val();
	if(flag) return;
	flag = true;
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/goods/updategoodsbrand')}",
		data : {
			'brand_id' : brand_id,
			'brand_name' : brand_name,
			'brand_initial' : brand_initial,
			'brand_recommend' : brand_recommend,
			'brand_pic' : brand_pic,
			'brand_ads' : brand_ads,
			'category_id_1' : category_id_1,
			'category_id_2' : category_id_2,
			'category_id_3' : category_id_3,
			'category_name' : category_name
		},
		success : function(data) {
			if (data["code"] > 0) {
				showTip("商品品牌修改成功","success");
				location.href = "{:__URL('ADMIN_MAIN/goods/goodsbrandlist')}";
			} else {
				showTip("商品品牌修改失败","error");
				flag = false;
			}
		}
	});
}

//图片上传
function imgUpload(event) {
	var fileid = $(event).attr("id");
	var id = $(event).next().attr("id");
	var data = { 'file_path' : UPLOADGOODSBRAND };
	uploadFile(fileid,data,function(res){
		if(res.code){
			$("#"+id).val(res.data);
			$("#text_" + id).val(res.data);
			$("#preview_" + id).attr("data-src",__IMG(res.data));
			showTip(res.message,"success");
		}else{
			showTip(res.message,"error");
		}
	});
}

function show_select() {
	$(".category_name").hide();
	$("#category_id_1").show();
}

function changeCategory(type,callBack) {
	var pid = $("#category_id_" + type).val();
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/goods/changecategory')}",
		data : { 'pid' : pid },
		success : function(data) {
			if (type == 1) {
				$("#category_id_2 option:gt(0)").remove();
				$("#category_id_3 option:gt(0)").remove();
			} else if (type == 2) {
				$("#category_id_3 option:gt(0)").remove();
			}
			var html = '';
			for (var i = 0; i < data.length; i++) {
				if(category_id_2 != "" && category_id_2 != 0 && category_id_2 == data[i]['category_id']){
					html += '<option value="'+data[i]['category_id']+'" selected="selected">' + data[i]['category_name'] + '</option>';
				}else if(category_id_3 != "" && category_id_3 != 0 && category_id_3 == data[i]['category_id']){
					html += '<option value="'+data[i]['category_id']+'" selected="selected">' + data[i]['category_name'] + '</option>';
					category_id_2 = "";//只执行一次
					category_id_3 = "";//只执行一次
				}else{
					html += '<option value="'+data[i]['category_id']+'">' + data[i]['category_name'] + '</option>';
				}
			}
			if (data.length > 0) {
				$("#category_id_" + (type + 1)).append(html);
				$("#category_id_" + (type + 1)).show();
			}
			if(callBack) callBack();
		}
	});
}
</script>
{/block}